package com.shopping_back.controller;


import org.springframework.web.bind.annotation.*;
import com.shopping_back.common.Result;
import lombok.RequiredArgsConstructor;
import com.shopping_back.service.IEvaluationService;
import com.shopping_back.pojo.dto.EvaluationSubmitDTO;
import com.shopping_back.common.Result;
import com.shopping_back.service.IEvaluationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品评价表 前端控制器
 * </p>
 *
 * @author Jack
 * @since 2025-07-12
 */
@RestController
@RequestMapping("/api/evaluation")
@RequiredArgsConstructor
public class EvaluationController {
    private final IEvaluationService evaluationService;

    /**
     * 管理员获取全部评论
     */
    @GetMapping("/admin/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Integer auditStatus) {

        return Result.success(evaluationService.listAll(pageNum, pageSize, auditStatus));
    }

    /**
     * 删评：把状态从 1 -> 0
     */
    @PostMapping("/admin/{id}/delete")
    public Result delete(@PathVariable Long id) {
        evaluationService.deleteByAdmin(id);
        return Result.success();
    }

    /**
     * 审核通过：0 -> 1
     */
    @PostMapping("/admin/{id}/approve")
    public Result approve(@PathVariable Long id) {
        evaluationService.approve(id);
        return Result.success();
    }

    /**
     * 买家提交商品评价
     */
    @PostMapping
    public Result submitEvaluation(@RequestBody EvaluationSubmitDTO dto) {
        evaluationService.submitEvaluation(dto, null);
        return Result.success("评价提交成功");
    }

    /**
     * 根据商品 SPU 查询评价分页
     */
    @GetMapping("/product")
    public Result listByProduct(@RequestParam Long spuId,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int size) {
        var pageData = evaluationService.getProductEvaluations(spuId, page, size);
        return Result.success(pageData, "查询成功");
    }

    @GetMapping("/evaluations")
    public Result getEvaluations(@RequestParam int page, @RequestParam int size, @RequestParam Long merchantId) {
        return Result.success(evaluationService.getEvaluations(merchantId, page, size));
    }
}
